package com.intelligentmeterreadingsys;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.intelligentmeterreadingsys.checkwork.activity.CheckWorkMainActivity;
import com.intelligentmeterreadingsys.otherequipment.OtherequipmentMainActivity;
import com.intelligentmeterreadingsys.report.activity.ReportMainActivity;
import com.intelligentmeterreadingsys.settingsys.activity.SettingSysActivity;
import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;
import com.intelligentmeterreadingsys.waterlist.activity.WaterListMainActivity;
import com.intelligentmeterreadingsys.waterlist.reviewdata.activity.ReviewMainActivity;

import dbhelper.DBOpenHelper;
import dbhelper.XmlNameHelper;

/**
 * 软件主界面
 * 
 * @author king
 * 
 */
public class MainActivity extends Activity implements OnClickListener, BDLocationListener {
    public Button button1;// 考勤

    public Button button2;// 系统设置

    public Button button3;// 退出系统

    public Button button4;// 表册管理

    public Button button5;// 复查管理

    public Button button6;// 工程管理

    public boolean isSafe = false;

    public SQLiteDatabase db;

    public SharedPreferenceUtil sp;

    public LocationClient client;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	setContentView(R.layout.main);

	button1 = (Button) findViewById(R.id.checkwork);
	button2 = (Button) findViewById(R.id.settingsys);
	button3 = (Button) findViewById(R.id.exitsys);
	button4 = (Button) findViewById(R.id.waterlist);
	button6 = (Button) findViewById(R.id.Report);
	button5 = (Button) findViewById(R.id.usermanager);
	button5.setOnClickListener(this);
	button6.setOnClickListener(this);
	button4.setOnClickListener(this);
	button3.setOnClickListener(this);
	button2.setOnClickListener(this);
	button1.setOnClickListener(this);
	TelephonyManager tm = (TelephonyManager) getSystemService(MainActivity.TELEPHONY_SERVICE);
	String imsi = tm.getSubscriberId();
	int sdk = Build.VERSION.SDK_INT;

	System.out.println("sdk---------->>>" + sdk);
	// 将手机卡IMSI存入XML
	sp = new SharedPreferenceUtil(this);
	sp.insertValue(XmlNameHelper.IMSI, imsi);

	String gpsdate = sp.getValue(XmlNameHelper.GPSDATE);
	if (gpsdate == null || gpsdate.equals("")) {
	    sp.insertValue(XmlNameHelper.GPSDATE, "0");
	    sp.insertValue(XmlNameHelper.GPSLATITUDE, "0.0");
	    sp.insertValue(XmlNameHelper.GPSLONGITUDE, "0.0");
	}
	sp.insertValue(XmlNameHelper.QUERYCOPYWATERSTATUS, "请选择");
	sp.insertValue(XmlNameHelper.QUERYWATERLIST, "请选择");
	sp.insertValue(XmlNameHelper.QUERYUSERNAME, "");
	sp.insertValue(XmlNameHelper.QUERYWATERADDS, "");
	sp.insertValue(XmlNameHelper.QUERYWATERNUMBER, "");
	String safetime = sp.getValue(XmlNameHelper.safetytime);
	if (safetime == null || safetime.equals("")) {
	    sp.insertValue(XmlNameHelper.isSafe, true);
	} else {
	    sp.insertValue(XmlNameHelper.isSafe, false);
	}
	initlocation();
	// //启动定时下载
	// Intent intent=new
	// Intent(MainActivity.this,WaterlistUploadService.class);
	//
	// startService(intent);
	//

	// Intent intent1=new Intent(MainActivity.this,
	// TimingLocationService.class);
	// startService(intent1);
	DBOpenHelper helper = new DBOpenHelper(this);
	db = helper.getReadableDatabase();
	db.close();
    }

    @Override
    protected void onDestroy() {
	System.out.println("onDestroy");
	if (db != null && db.isOpen()) {
	    db.close();
	}
	if (client != null && client.isStarted()) {
	    client.stop();
	}
	super.onDestroy();
    }

    @Override
    public void onClick(View v) {
	Intent intent = null;
	switch (v.getId()) {
	case R.id.checkwork:
	    intent = new Intent(MainActivity.this, CheckWorkMainActivity.class);
	    startActivity(intent);
	    break;
	case R.id.settingsys:
	    intent = new Intent(MainActivity.this, SettingSysActivity.class);
	    startActivity(intent);
	    break;

	case R.id.exitsys:
	    intent = new Intent(MainActivity.this, OtherequipmentMainActivity.class);
	    // Bundle bundle=new Bundle();
	    // bundle.putString("waterid", "");
	    // intent.putExtras(bundle);
	    // intent.putExtra("waterid", "");
	    startActivity(intent);
	    break;
	case R.id.waterlist:
	    intent = new Intent(MainActivity.this, WaterListMainActivity.class);
	    startActivity(intent);
	    break;
	case R.id.Report:

	    intent = new Intent(MainActivity.this, ReportMainActivity.class);
	    startActivity(intent);
	    break;
	case R.id.usermanager:
	    intent = new Intent(MainActivity.this, ReviewMainActivity.class);
	    startActivity(intent);
	    break;
	default:
	    break;
	}

    }

    public boolean showDialog() {

	LayoutInflater factory = LayoutInflater.from(this);
	final View textEntryView = factory.inflate(R.layout.dialog, null);
	final EditText editTextPass = (EditText) textEntryView.findViewById(R.id.txt_password);

	AlertDialog.Builder ad1 = new AlertDialog.Builder(this);
	ad1.setIcon(android.R.drawable.ic_dialog_info);
	ad1.setView(textEntryView);
	ad1.setTitle("安全验证");
	ad1.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() {

	    @Override
	    public void onClick(DialogInterface dialog, int which) {
		String pass = editTextPass.getEditableText().toString();
		SharedPreferenceUtil sh = new SharedPreferenceUtil(MainActivity.this);
		if (pass.equals(sh.getValue(XmlNameHelper.passWord))) {
		    isSafe = true;
		} else {
		    Toast.makeText(MainActivity.this, "密码错误", 1000).show();
		    isSafe = false;
		}

	    }
	});
	ad1.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {

	    @Override
	    public void onClick(DialogInterface dialog, int which) {
		isSafe = false;

	    }
	});
	ad1.create();
	ad1.show();
	return isSafe;

    }

    public void SafeShow(Class<?> activityClass) {
	Intent intent = null;
	SharedPreferenceUtil sh = new SharedPreferenceUtil(MainActivity.this);
	boolean b = sh.getBoolean(XmlNameHelper.isSafe);
	String pass = sh.getValue(XmlNameHelper.passWord);
	if (!pass.equals("") || !b) {
	    boolean bb = showDialog();
	    if (!bb) {
		return;
	    } else {
		intent = new Intent(this, activityClass);
		startActivity(intent);
	    }
	}
	intent = new Intent(this, activityClass);
	startActivity(intent);
    }

    @Override
    public void onReceiveLocation(BDLocation location) {

	saveCity(location);
    }

    @Override
    public void onReceivePoi(BDLocation arg0) {
	// TODO Auto-generated method stub

    }

    public void saveCity(BDLocation location) {
	if (location != null && location.getLocType() == 161) {
	    String city = location.getCity();
	    city = city.substring(0, city.length() - 1);
	    sp.insertValue(XmlNameHelper.CITY, city);

	    client.stop();
	}
    }

    /**
     * 初始化定位
     */
    public void initlocation() {

	client = new LocationClient(this);
	LocationClientOption option = new LocationClientOption();
	// 打开GPS
	option.setOpenGps(true);

	// 设置坐标类型
	option.setCoorType("bd09ll");

	// 设置网络优先

	option.setPriority(LocationClientOption.NetWorkFirst);

	// 设置产品线名称
	option.setProdName("waterSys");
	option.disableCache(true);// 禁止启用缓存定位
	// 设置定位定时时间
	option.setScanSpan(10000);
	client.setLocOption(option);
	client.registerLocationListener(this);
	client.start();

    }

}
